//
//  CompetionBottomView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/9.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CompetionBottomView.h"
#import "CompetionCell.h"
#import "InstituteModel.h"
@implementation CompetionBottomView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createCollectionUI];
    }
    return self;
}
/**
 创建collectioview的布局
 */
-(void)createCollectionUI{
    self.collectionView.scrollsToTop = YES;
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumLineSpacing = scaleLeftAndRight(25);
    self.flowLayout.minimumInteritemSpacing = scaleTopAndBottom(20);
//    self.collectionView.showsVerticalScrollIndicator = NO;
//    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.flowLayout.itemSize = CGSizeMake(scaleLeftAndRight(65), scaleTopAndBottom(100));
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.frame.size.height) collectionViewLayout:self.flowLayout];
    [self addSubview:self.collectionView];
    [self.collectionView registerClass:[CompetionCell class] forCellWithReuseIdentifier:@"CompetionCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataCataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CompetionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CompetionCell" forIndexPath:indexPath];
    for (UIView *subViews in cell.contentView.subviews) {
        [subViews removeFromSuperview];
    }
    cell.instiModl = self.dataCataArray[indexPath.row];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0,scaleLeftAndRight(24), 0, scaleLeftAndRight(24));
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    InstituteModel * model = self.dataCataArray[indexPath.row];
    if (self.goBlock) {
        self.goBlock(model.Id);
    }
    NSLog(@"点击了哦");
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    scrollView.showsHorizontalScrollIndicator = NO;
}
-(NSMutableArray *)dataCataArray{
    if (_dataCataArray == nil) {
        _dataCataArray = [[NSMutableArray alloc] init];
        [self getInformationData];
    }
    return _dataCataArray;
}

-(void)getInformationData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/institutionApp/institutionList";
    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:@"0",@"sortord",@"20",@"showCount", nil];
    [manager POST:url parameters:parma progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *listArray = jsonDic[@"list"];
        for (NSDictionary *dic in listArray) {
            InstituteModel *model = [InstituteModel modelWithDictionary:dic];
            [self.dataCataArray addObject:model];
        }
        [self.collectionView reloadData];
        NSLog(@"字典数据%@",jsonDic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

@end
